#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import nonebot
from nonebot.adapters.onebot.v11 import Adapter as ONEBOT_V11Adapter
from nonebot import on_keyword
from nonebot.typing import T_State
from nonebot.adapters.onebot.v11 import GroupMessageEvent, Bot, Message, MessageSegment, Event
import requests
import json
from nonebot import require

# Custom your logger
#
# from nonebot.log import logger, default_format
# logger.add("error.log",
#            rotation="00:00",
#            diagnose=False,
#            level="ERROR",
#            format=default_format)

# You can pass some keyword args config to init function
nonebot.init()
app = nonebot.get_asgi()

driver = nonebot.get_driver()
driver.register_adapter(ONEBOT_V11Adapter)
nonebot.load_builtin_plugins("echo")
nonebot.load_plugin("nonebot_plugin_remake")
housekeeper = require("nonebot_plugin_apscheduler").scheduler


# Please DO NOT modify this file unless you know what you are doing!
# As an alternative, you should use command `nb` or modify `pyproject.toml` to load plugins
# nonebot.load_from_toml("pyproject.toml")

# Modify some config / config depends on loaded configs
#
# config = driver.config
# do something...
# 实现定时任务
# hour里面的写的是 时间具体是几点开启这个定时任务
@housekeeper.scheduled_job("cron", hour='17', minute='00', id="body_temperature")
async def body_temperature():
    (bot,) = nonebot.get_bots().values()
    await bot.send_msg(
        message_type="public",
        user_id=int('xxxxx'),  # int（）中填写qq群号码
        message="体温上报一下！"  # 写上你要写的消息 写的时候不需要加引号 整形就行
    )
    await bot.send_msg(
        message_type="group",
        # 群号
        group_id=int('xxxxx'),
        message='体温上报一下'
    )


# 决定以何种方式进行处理(#开头或者是以什么开头)
# 这里写上之后就可以进行对话
cici = on_keyword({'#'})


@cici.handle()
async def sj(bot: Bot, event: Event, state: T_State):
    anses = str(event.get_message()).strip()
    ansek = anses.strip('#')
    url = f'http://api.qingyunke.com/api.php?key=free&appid=0&msg={ansek}'
    k = requests.get(url)
    hua = json.loads(k.text)
    # print(hua)
    ans = hua['content']
    l = '菲菲'
    if l in ans:
        ansl = ans.replace('菲菲', '辞辞')
        await cici.finish(Message(f'{ansl}'))
    else:
        await cici.finish(Message(f'{ans}'))


if __name__ == "__main__":
    nonebot.logger.warning("Always use `nb run` to start the bot instead of manually running!")
    nonebot.run(app="__mp_main__:app")
